/*
 * @Author: xinyang3
 * @Date: 2021-09-02 15:39
 * @Descripttion: awesome description
 * @LastEditors: xinyang3
 * @LastEditTime: 2021-11-16 17:58
 */
// import fs from 'fs';
// import path from 'path';
import * as showdown from 'showdown';
// const server_file_path = path.resolve(__dirname, '../md_docs/');
const warn = (content) => console.warn(content);

const showdownMd = showdown.default;
// md转换器
showdownMd.extension('myext', function (html, converter, options) {
  let container = document.createElement('div');
  container.innerHTML = html;
  let images = container.querySelector('img');
  let image;
  images.forEach((item) => {
    console.log(item);
  });
  return container.innerHTML;
});

const converter = new showdownMd.Converter();
converter.setFlavor('github');

/**
 * 将md字符流转换成html字符串
 * @param {*} mddoc
 */
function convertToHtml(mddoc) {
  if (!mddoc) return warn('mddoc is null.');
  return converter.makeHtml(mddoc);
}

export { convertToHtml };

/**
 * 发送html字符串
 * @param {*} name
 * @param {*} cb
 */
// function parsenFile(name) {
//   if (!name || !name.includes('.md'))
//     return warn(`md file ${name} is not exist.`);

//   var paths = path.resolve(server_file_path, name);

//   return new Promise((resolve, reject) => {
//     fs.access(paths, fs.constants.R_OK, (err) => {
//       if (err) {
//         warn('file is not existed.');
//         reject(null);
//         return;
//       }

//       fs.readFile(paths, 'utf8', (e, data) => {
//         if (e) {
//           warn(`file read error.`);
//           reject(null);
//           return;
//         }
//         var html = convertToHtml(data);
//         resolve(html);
//       });
//     });
//   });
// }
